package com.lvxh.exp.expmemo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lvxh.exp.bean.BaseController;
import com.lvxh.exp.bean.GridResultHolder;
import com.lvxh.exp.expmemo.dao.holder.Expmemo;
import com.lvxh.exp.expmemo.service.ExpmemoService;

@RequestMapping("/expmemo")
@Controller
public class ExpmemoController extends BaseController
{
    private static final Logger log = Logger.getLogger(ExpmemoController.class);

    @Autowired
    private ExpmemoService expmemoService;

    @RequestMapping("/initexpmemo")
    public ModelAndView initExpmemo()
    {
        log.info("start method initExpmemo");
        log.info("end method initExpmemo");
        return new ModelAndView("expmemo/viewExpmemo");
    }
    @RequestMapping("/getGrid")
    @ResponseBody
    public GridResultHolder getGrid(Expmemo bean)
    {
        log.info("start method getGrid");
        GridResultHolder result = new GridResultHolder();
        int count = expmemoService.getCountOfSummary(bean);
        int sum = procesPage(bean, count);
        result.setResult(expmemoService.getListOfSummary(bean));
        result.setPageCount(sum);
        log.info("end method getGrid");
        return result;
    }
    @RequestMapping("/initadd")
    public ModelAndView initAdd()
    {
        log.info("start method initAdd");
        log.info("end method initAdd");
        return new ModelAndView("expmemo/addExpmemo");
    }
    @RequestMapping("/add")
    public ModelAndView add(Expmemo bean)
    {
        int result=0;
        log.info("start method add");
        Map<String,String>map = new HashMap<String,String>();
       
        try
        {
            result = expmemoService.insert(bean);
        }
        catch (Exception e)
        {
            log.error(e.getMessage(),e);
        }
        if(result > 0)
        {
            map.put("backUrl", "/expmemo/initexpmemo.action");
            log.info("end method add");
            return new ModelAndView("global/success",map);
        }
        else
        {
            log.info("end method add");
            return new ModelAndView("global/failed");
        }
       
    }
    @RequestMapping("/initmodify")
    public ModelAndView initModify(String delFlag)
    {
        log.info("start method initModify");
        Map<String,Object>map = new HashMap<String,Object>();
        Expmemo bean = new Expmemo();
        bean.setId(Integer.parseInt(delFlag));
        List <Expmemo>list = expmemoService.getList(bean);
        if(list == null || list.isEmpty())
        {
            log.info("end method initModify");
            return new ModelAndView("global/failed");
        }
        else
        {
            map.put("bean", list.get(0));
            log.info("end method initModify");
            return new ModelAndView("expmemo/modifyExpmemo",map);
        }
    }

    @RequestMapping("/modify")
    public ModelAndView modify(Expmemo bean)
    {
        int result=0;
        log.info("start method modify");
        Map<String,String>map = new HashMap<String,String>();
       
        try
        {
            result = expmemoService.update(bean);
        }
        catch (Exception e)
        {
            log.error(e.getMessage(),e);
        }
        if(result > 0)
        {
            map.put("backUrl", "/expmemo/initexpmemo.action");
            log.info("end method modify");
            return new ModelAndView("global/success",map);
        }
        else
        {
            log.info("end method modify");
            return new ModelAndView("global/failed");
        }
       
    }
    @RequestMapping("/del")
    public ModelAndView del(String delFlag)
    {
        int result=0;
        log.info("start method modify");
        Map<String,String>map = new HashMap<String,String>();
       
        try
        {
            if(delFlag !=null && !"".equals(delFlag))
            {
                Expmemo bean = new Expmemo();
                bean.setId(Integer.parseInt(delFlag.split("\\|")[1]));
                result = expmemoService.delete(bean);
            }
        }
        catch (Exception e)
        {
            log.error(e.getMessage(),e);
        }
        if(result > 0)
        {
            map.put("backUrl", "/expmemo/initexpmemo.action");
            log.info("end method modify");
            return new ModelAndView("global/success",map);
        }
        else
        {
            log.info("end method modify");
            return new ModelAndView("global/failed");
        }
       
    }    @RequestMapping("/delall")
    public ModelAndView delAll(String delFlag)
    {
        

        int result=0;
        log.info("start method modify");
        Map<String,String>map = new HashMap<String,String>();
       
        try
        {
            if(delFlag !=null && !"".equals(delFlag))
            {
                
                List<Expmemo> list = new ArrayList<Expmemo>();
                String str[]= delFlag.split("\\|");
                for(int i=1;i<str.length;i++)
                {
                    Expmemo bean = new Expmemo();
                    bean.setId(Integer.parseInt(str[i]));
                    list.add(bean);
                }
                result = expmemoService.delete(list);
            }
        }
        catch (Exception e)
        {
            log.error(e.getMessage(),e);
        }
        if(result > 0)
        {
            map.put("backUrl", "/expmemo/initexpmemo.action");
            log.info("end method modify");
            return new ModelAndView("global/success",map);
        }
        else
        {
            log.info("end method modify");
            return new ModelAndView("global/failed");
        }
       
    
       
    }
}
